package com.xgr.wonderful.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.google.gson.Gson;
import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.Action;
import com.xgr.wonderful.R;
import com.xgr.wonderful.activity.base.BasePageActivity;
import com.xgr.wonderful.constant.Constant;
import com.xgr.wonderful.entity.User;
import com.xgr.wonderful.utils.HttpProxy;
import com.xgr.wonderful.utils.LocalUserUtils;
import com.xgr.wonderful.utils.LogUtils;
import com.xgr.wonderful.utils.ToastFactory;
import com.xgr.wonderful.utils.HttpProxy.IHttpRequestListener;

/**
 * 更改用户名，成功后finish
 * @author lsj
 *
 */
public class MyInfoModifyActivity  extends BasePageActivity{
	
	private ActionBar mActionBar ;
	private EditText mEditname ;
	private User mUser ;
	
	private HttpProxy  httpProxy ;
	
	@Override
	protected void setLayoutView() {
		setContentView(R.layout.activity_modifyinfo) ;
	}

	@Override
	protected void findViews() {
		mActionBar = (ActionBar)findViewById(R.id.actionbar_modifyinfo) ;
		mEditname = (EditText)findViewById(R.id.modifyname_tv) ;
	}

	@Override
	protected void setupViews(Bundle bundle) {
		mActionBar.setTitle("更改用户名") ;
		mActionBar.setDisplayHomeAsUpEnabled(true) ;
		mActionBar.setHomeAction(new Action() {
			
			@Override
			public void performAction(View view) {
				finish() ;
			}
			@Override
			public int getDrawable() {
				return R.drawable.weix_logo_blank;
			}
		}) ;
		mActionBar.addAction(new Action() {
			
			@Override
			public void performAction(View view) {
				//确定按钮
				if (mUser == null || mUser.getUsername()== null){
					ToastFactory.getToast(mContext, "您还没有注册或者登录").show() ;
					return ;
				}
				String name = mEditname.getText().toString().trim() ;
				if (name == null || name.equals(mUser.getUsername())){
					ToastFactory.getToast(mContext, "请更改用户名").show() ;
				}else if (name.length()<3){
					ToastFactory.getToast(mContext, "用户名长要多于3个字符").show() ;
				}else {
					User user = new User(mUser.getID(), 
							null, name, null, null) ;
					modifyName(user) ;
				}
			}
			@Override
			public int getDrawable() {
				// TODO Auto-generated method stub
				return R.drawable.ic_ok;
			}
		});
		mUser = LocalUserUtils.getInstance().getLocalUser() ;
		if (mUser!= null&& mUser.getUsername()!= null){
			mEditname.setText(mUser.getUsername());
 		}
		httpProxy = new HttpProxy() ;
		
	}
	
	private void modifyName (User user ){
		
		String userJson = new Gson().toJson(user) ;
		
		httpProxy.updateUser(userJson, new IHttpRequestListener() {
			
			@Override
			public void OnHttpRequestSuccessful(String result, int errCode) {
				if (errCode == Constant.ERR_HTTP_REQUEST_SUCCESS){
					ToastFactory.getToast(mContext, "更改用户名成功").show() ;
					cancelProgressDialog() ;
					
					mUser.setUsername(mEditname.getText().toString().trim()) ;
					LocalUserUtils.getInstance().saveLocalUser(mContext, mUser ) ;
					finish() ;
				}else if (errCode == Constant.ERR_HTTP_REQUEST_FAILURE) {
					cancelProgressDialog() ;
					ToastFactory.getToast(mContext, "更改失败").show() ;
				}
			}
			
			@Override
			public void OnHttpRequestStart() {
				showProgressDialog("正在更新用户名", false) ;
			}
			
			@Override
			public void OnHttpRequestFailure(String info) {
				cancelProgressDialog() ;
				LogUtils.i(TAG, "fail info:"+ info);
				ToastFactory.getToast(mContext, "更新用户名失败").show() ;
			}
		}) ;
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		
	}

}
